package com.mmp.main.servlet.report;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.PPMTableRecord;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class ViewPPMTable extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int siteId = getSessionSiteId(request);
		// int locationId = getSessionLocationId(request);
		int category = WebUtil.getParamValueAsInteger(request, "category", 0);

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		// int i = 1;

		String query = "SELECT name, (SELECT name FROM lookup_device_category WHERE id = category_id) AS category, category_id, subcontractor, model, pm_visits_months, (pm_annual_visits_no * 3) AS contract_visits_no, count(id) AS device_no_in_site"
				+ " FROM hospital_device WHERE site_id = ? AND status NOT IN (?,?) AND within_warranty = ? AND within_contract = ?";

		map.put(1, siteId);
		map.put(2, Enums.DEVICE_STATUS.SCRAPPED.getStatus());
		map.put(3, Enums.DEVICE_STATUS.TRANSFERRED.getStatus());
		map.put(4, false);
		map.put(5, true);

		if (category != 0) {
			query += " AND category_id = ?";
			map.put(6, category);
		}
		query += " GROUP BY category_id, subcontractor, model ORDER BY category_id, subcontractor, name";

		List list = dbQueryManager.executeNativeQueryForSearch(query, map);

		List<PPMTableRecord> records = null;

		if (list != null && !list.isEmpty()) {
			records = new ArrayList<PPMTableRecord>();

			for (int i = 0; i < list.size(); i++) {
				Object[] r = (Object[]) list.get(i);

				PPMTableRecord tr = new PPMTableRecord();
				tr.setDeviceName((String) r[1] + " - " + (String) r[0]);
				tr.setSubcontractor((String) r[3]);
				tr.setPmVisitsMonths((String) r[5]);
				tr.setContractVisitsNo(((BigInteger) r[6]).intValue());
				tr.setDeviceNoInSite(((BigInteger) r[7]).intValue());
				records.add(tr);
			}
		}
		// String jpql =
		// "SELECT h FROM HospitalDevice h WHERE h.status NOT IN ("
		// + Enums.DEVICE_STATUS.SCRAPPED.getStatus() + "," +
		// Enums.DEVICE_STATUS.TRANSFERRED.getStatus()
		// + ") AND h.site.id = " + siteId;

		// if (locationId != 0) {
		// jpql += " AND h.hospital.id = ?";
		// map.put(i++, locationId);
		// }

		// if (category != 0) {
		// jpql += " AND h.category.id = ? ";
		// map.put(i++, category);
		// }
		//
		// jpql += " ORDER BY h.category.id, h.name ";
		//
		// List<HospitalDevice> deviceList = dbm.queryJPQL(jpql, map,
		// HospitalDevice.class);

		if (records == null || records.isEmpty()) {
			announceErorrMessage(request, response,
					"عفوا .. لا توجد  أجهزة خارج الضمان  من الفئة المطلوبة ويتضمنها العقد");
			return;
		}

		HashMap params = new HashMap();
		params.put(Default.REPORT_TITLE_PARAM_NAME, Enums.REPORT_TITLE.PPM_TABLE.getReportTitle());
		params.put("deviceList", records);
		viewPDFReport("rt7.jasper", null, params, request, response);

	}
}
